//评论相关接口

import 'package:tiktokadult/hj_model/comment/comment_list_res.dart';
import 'package:tiktokadult/hj_model/comment/comment_model.dart';
import 'package:tiktokadult/hj_model/comment/reply_model.dart';
import 'package:tiktokadult/hj_model/comment/user_comment_res.dart';
import 'package:tiktokadult/hj_model/list_base_model.dart';
import 'package:tiktokadult/hj_utils/http_manager.dart';

class CommentService {
  // //一级评论的全部列表
  static Future<CommentListRes?> getCommentList(
    String? objID,
    String? curTime,
    int? pageNumber,
    int? pageSize, {
    String? objType, //video：视频(默认) cartoon：动漫 section：专题
  }) async {
    final param = <String, dynamic>{
      'objID': objID,
      'curTime': curTime,
      'pageNumber': pageNumber,
      'pageSize': pageSize,
      'objType': objType,
    };
    param.removeWhere((k, v) => v == null);
    final result = await httpManager.fetchResponseByGET(
      '/comment/list',
      param: param,
    );
    return CommentListRes.fromJson(result.data);
  }

  //发表评论(1)
  //Address.SEND_COMMENT) objType 评论对象类型 video：视频(默认) cartoon：动漫 section：专题 driftBottle：树洞纸条
  // quote这个是评论引用资源，如视频，图集等数据，需要传quote相关数据。
  static Future<CommentModel?> sendComment(
    String? objID,
    int? level,
    String? content,
    String objType, {
    String? quoteID, //引用id
    String? quoteImg, //引用资源图片
    String? quoteTitle, //引用资源标题
    String? quoteType, //引用类型 vid视频 col用户播单 sec官方播单
    String? image, // 图片资源
  }) async {
    final param = {
      'objID': objID,
      'level': level,
      'content': content,
      "objType": objType,
      "quoteID": quoteID,
      "quoteImg": quoteImg,
      "quoteTitle": quoteTitle,
      "quoteType": quoteType,
      'image': image,
    };
    param.removeWhere((k, v) => v == null);
    final result = await httpManager.fetchResponseByPOST(
      '/comment/send',
      param: param,
    );
    if (result.data is Map) {
      return CommentModel.fromJson(result.data);
    }
    return null;
  }

  //此接口为了兼容后台的树洞纸条回应功能，只需要返回是否回应成功
  //Address.SEND_COMMENT) objType 评论对象类型 video：视频(默认) cartoon：动漫 section：专题 driftBottle：树洞纸条
  // quote这个是评论引用资源，如视频，图集等数据，需要传quote相关数据。
  static Future<bool?> commentSend(
    String? objID,
    int? level,
    String? content,
    String objType, {
    String? quoteID, //引用id
    String? quoteImg, //引用资源图片
    String? quoteTitle, //引用资源标题
    String? quoteType, //引用类型 vid视频 col用户播单 sec官方播单
  }) async {
    final param = {
      'objID': objID,
      'level': level,
      'content': content,
      "objType": objType,
      "quoteID": quoteID,
      "quoteImg": quoteImg,
      "quoteTitle": quoteTitle,
      "quoteType": quoteType,
    };
    param.removeWhere((k, v) => v == null);
    final result = await httpManager.fetchResponseByPOST(
      '/comment/send',
      param: param,
    );
    return result.isSuccess;
  }

  /// 回复
  ///sendReply 回复评论，
  ///
  ///[quoteID] 引用资源id
  ///[objType] 是业务类型， 根据项目业务来定。
  ///[cid]（评论id）， [rid]（回复id）， [toUserID] (回复评论人的id), 只有回复评论才需要这个参数。
  ///[objID] 对象id(如视频详情，传视频详情id)。
  ///评论 [level] = 1； 回复 [level] = 2
  //  */
  static Future<ReplyModel?> sendReply(
    String? objID,
    int? level,
    String? content, {
    String? cid,
    String? rid,
    int? toUserID,
    dynamic objType,
    String? quoteID,
    String? quoteImg,
    String? quoteTitle,
    String? quoteType,
    String? image, // 图片资源
  }) async {
    final param = {
      'cid': cid,
      'content': content,
      'level': level,
      'objID': objID,
      "objType": objType,
      "quoteID": quoteID,
      "quoteImg": quoteImg,
      "quoteTitle": quoteTitle,
      "quoteType": quoteType,
      'rid': rid,
      'toUserID': toUserID,
      'image': image,
    };
    param.removeWhere((k, v) => v == null);
    final result = await httpManager.fetchResponseByPOST(
      '/comment/send',
      param: param,
    );
    if (result.data is Map) {
      return ReplyModel.fromJson(result.data);
    }
    return null;
  }

  static Future<bool> sendMessage({required int takeUid, List<String>? imgUrl, String? content, String? objID, int? objType}) async {
    Map<String, dynamic> param = {};
    if (objID != null) {
      param['objID'] = objID;
      if (objType != null) {
        param['objType'] = objType;
        param['toUid'] = takeUid;
      }
      if (content != null) {
        param['text'] = content;
      }
      if (imgUrl != null) {
        param['pics'] = imgUrl;
      }
    } else {
      param['takeUid'] = takeUid;
      if (content != null) {
        param['content'] = content;
      }
      if (imgUrl != null) {
        param['imgUrl'] = imgUrl;
      }
    }
    var url = objID != null && objType != null ? "/store/msg/chat" : "/msg/priLetter/add";
    final result = await httpManager.fetchResponseByPOST(url, param: param);
    return result.isSuccess;
  }

  ///评论的全部回复列表
  static Future<ReplyListRes?> getReplyList(
    String? objID,
    String? cmtId,
    String? curTime,
    int? pageNumber,
    int? pageSize, [
    String? fstID,
  ]) async {
    final param = <String, dynamic>{
      'objID': objID,
      'cmtId': cmtId,
      'curTime': curTime,
      'pageNumber': pageNumber,
      'pageSize': pageSize,
      'fstID': fstID
    };
    param.removeWhere((k, v) => v == null);
    final result = await httpManager.fetchResponseByGET(
      '/comment/info',
      param: param,
      jsonTransformation: (json) => ReplyListRes.fromJson(json),
    );
    return result.data;
  }

  static Future<ListBaseModel<UserCommentItem>?> getUserCommentList(int? uid, int? pageNumber, int? pageSize) async {
    final param = <String, dynamic>{
      'pageNumber': pageNumber,
      'pageSize': pageSize,
      'uid': uid,
    };
    param.removeWhere((k, v) => v == null);
    final result = await httpManager.fetchResponseByGET(
      '/mine/comment',
      param: param,
      jsonTransformation: (json) => ListBaseModel<UserCommentItem>.fromJson(json),
    );
    return result.data;
  }
}
